﻿using FastCloud.Base.Desgin;
using FastCloud.Extend.Entity;
using FastUtil.Data;
using FastUtil.Extra;
using System;
using System.Collections.Generic;
using System.Text;

namespace FastCloud.Extend.DB
{
    public class DB_Task : BaseDBML<Frame_Task>
    {
        public override async void Insert(Frame_Task Entity)
        {
            if (string.IsNullOrEmpty(Entity.TaskGuid))
            {
                Entity.TaskGuid = GenerateUtil.GenerateUnitGuid();
            }
            Entity.AddDate = DateTime.Now;
            base.Insert(Entity);
            if (Entity.TaskStatus==(int)FastCloud.Base.Enum.Extend.TaskStatus.运行)
            {
              await QuartzUtil.StartJob(Entity.TaskType,Entity.TaskName, Entity.TaskTag, Entity.JobClassName, Entity.TaskExecutePeriod, Entity.TaskExecuteUnit, Entity.CornExplain);
            }
        }

        public override async void UpdateByID(Frame_Task Entity)
        {
            base.UpdateByID(Entity);
            if (Entity.TaskStatus == (int)FastCloud.Base.Enum.Extend.TaskStatus.运行)
            {
                await QuartzUtil.StartJob(Entity.TaskType, Entity.TaskName, Entity.TaskTag, Entity.JobClassName, Entity.TaskExecutePeriod, Entity.TaskExecuteUnit, Entity.CornExplain);
            }
            else
            {
                await QuartzUtil.PauseJob(Entity.TaskName, Entity.TaskTag);
            }
        }

        public override async void DeleteByID(string UnitGuid)
        {
            Frame_Task Entity = SelectOne(UnitGuid);
            await QuartzUtil.RemoveJob(Entity.TaskName, Entity.TaskTag);
            base.DeleteByID(UnitGuid);
        }
    }
}
